using System;
using System.Collections.Generic;
using System.Text;
using Rapier.Kodachi.Shared;
using System.Drawing;
using System.IO;

namespace Rapier.Kodachi.Firefox
{
    public class FirefoxCatalog
    {
        private Plugin plugin;
        private LinkDataSet links;
        private Bitmap bookmarkImage;

        public FirefoxCatalog(Plugin plugin)
        {
            this.plugin = plugin;
            bookmarkImage = (Bitmap)Rapier.Kodachi.Firefox.Properties.Resources.ResourceManager.GetObject("bookmark");
        }

        public void LoadItems()
        {
            try
            {
                string masterFileName = String.Empty;
                links = new LinkDataSet();
                string tempFile = Path.GetTempFileName();

                if (FirefoxPlugin.FirefoxSettings.UseDefault)
                {
                    //TODO: write logic to get default booksmarks
                    //TODO: write logic in here somewhere to reload using a filewatcher whe the html changes
                    //TODO: write logic for syncing FF->IE and back?
                    //masterFileName = @"C:\Users\David\AppData\Roaming\Mozilla\Firefox\Profiles\hpwjppz4.default\bookmarks.html";
                    masterFileName = String.Empty;
                }
                else
                {
                    //TODO: maybe have them browse for the folder, so get the file here?
                    masterFileName = FirefoxPlugin.FirefoxSettings.SpecificFirefoxProfilePath;
                }

                if (String.IsNullOrEmpty(masterFileName))
                    return;

                File.Copy(masterFileName, tempFile, true);

                string[] masterFileLines = System.IO.File.ReadAllLines(tempFile);
                MozillaFile masterFile = new MozillaFile(masterFileLines);

                ParseNodes(masterFile.Root);
                AddLinksToItemList();
            }
            catch
            {
                FirefoxPlugin.FirefoxSettings.UseDefault = false;
                FirefoxPlugin.FirefoxSettings.SpecificFirefoxProfilePath = String.Empty;
            }
        }

        private void AddLinksToItemList()
        {
            if (links != null && links.Links.Count > 0)
            {
                foreach (LinkDataSet.LinksRow row in links.Links)
                {
                    //TODO: Attach some sort of useful icon to this bad boy...
                    Item item = new Item(plugin, "File", row.Name, "Firefox Favorite");
                    item.Properties["Path"] = row.Link;
                    item.GetImage = delegate { return bookmarkImage; };
                    plugin.Host.AddItem(item);
                }
            }
        }

        private void ParseNodes(MozillaNode node)
        {
            if (node.HasChildren)
            {
                foreach (MozillaNode child in node.GetChildren())
                {
                    if (child.IsFolder)
                        ParseNodes(child);
                    else if (child.IsLink)
                    {
                        links.Links.Rows.Add(new object[] { child.Name, child.Href });
                    }
                }
            }
        }
    }
}
